package com.ssy.lingxi.pay.controller.feign;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.api.dto.request.assetAccount.MemberAssetAccountAddRequest;
import com.ssy.lingxi.pay.api.dto.request.assetAccount.MemberAssetAccountUpdateRequest;
import com.ssy.lingxi.pay.api.model.vo.request.FrozenCreditVO;
import com.ssy.lingxi.pay.common.CommonBooleanEnum;
import com.ssy.lingxi.pay.entity.assetAccount.MemberAssetAccount;
import com.ssy.lingxi.pay.service.ICreditService;
import com.ssy.lingxi.pay.service.assetAccount.IMemberAssetAccountService;
import com.ssy.lingxi.pay.service.eAccount.IEAccountService;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 同步资金账户
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@RestController
@RequestMapping("/pay/synchAssetAccount")
public class AssetAccountFeignController extends BaseController {

    @Autowired
    private IMemberAssetAccountService memberAssetAccountService;

    @Resource
    private IEAccountService eAccountService;

    @Autowired
    private ICreditService creditService;

    /**
     * 新增会员资金账户
     * @param memberAssetAccountRequestList 会员资金账户实体
     * @return
     */
    @RequestMapping(value = "/saveMemberAssetAccount", method = RequestMethod.POST)
    public Wrapper<String> saveMemberAssetAccount(@RequestBody List<MemberAssetAccountAddRequest> memberAssetAccountRequestList) {
        List<MemberAssetAccount> memberAssetAccountList = this.modelMapper.map(memberAssetAccountRequestList, new TypeToken<List<MemberAssetAccount>>() {
        }.getType());
        return Wrapper.success(memberAssetAccountService.saveOrUpdateMemberAssetAccount(memberAssetAccountList));
    }

    /**
     * 修改会员资金账户状态
     * @param memberAssetAccountUpdateRequest 会员资金账户实体
     * @return
     */
    @RequestMapping(value = "/updateMemberAssetAccount", method = RequestMethod.POST)
    public Wrapper<String> updateMemberAssetAccount(@RequestBody MemberAssetAccountUpdateRequest memberAssetAccountUpdateRequest) {
        Long memberId = memberAssetAccountUpdateRequest.getMemberId();
        Long memberRoleId = memberAssetAccountUpdateRequest.getMemberRoleId();
        Integer memberStatus = memberAssetAccountUpdateRequest.getMemberStatus();
        Long parentMemberId = memberAssetAccountUpdateRequest.getParentMemberId();
        Long parentMemberRoleId = memberAssetAccountUpdateRequest.getParentMemberRoleId();

        // 冻结/解冻授信
        FrozenCreditVO frozenCreditVO = new FrozenCreditVO();
        frozenCreditVO.setMemberId(memberId);
        frozenCreditVO.setRoleId(memberRoleId);
        frozenCreditVO.setParentMemberId(parentMemberId);
        frozenCreditVO.setParentMemberRoleId(parentMemberRoleId);
        if (memberStatus == 1) {
            frozenCreditVO.setStatus(CommonBooleanEnum.NO.getCode());
        } else if (memberStatus == 2) {
            frozenCreditVO.setStatus(CommonBooleanEnum.YES.getCode());
        }
        creditService.frozenCredit(frozenCreditVO);

        //冻结/解冻e账户的会员状态
        eAccountService.updateMemberAssetAccount(memberId, memberRoleId, memberStatus);

        return Wrapper.success(memberAssetAccountService.updateMemberAssetAccount(memberAssetAccountUpdateRequest));
    }

}
